package com.taysear.parcel;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.taysear.parcel.domain.Bus;
import com.taysear.parcel.domain.Employee;
import com.taysear.parcel.domain.LoadingsheetStatus;
import com.taysear.parcel.domain.Route;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.Substation;
import com.taysear.parcel.util.Constants;
import com.vaadin.data.Container;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.themes.Runo;

@SuppressWarnings("serial")
public class LoadingsheetTable extends Table {

	private ParcelApplication app;
	LoadingsheetWindow loadingsheetWindow;

	public LoadingsheetTable(final ParcelApplication app, String captopn, Container loadingsheets, String [] columns, final LoadingsheetWindow loadingsheetWindow) {
		super(captopn, loadingsheets);
		this.app = app;
		this.loadingsheetWindow = loadingsheetWindow;
		
		addGeneratedColumn("Loadingsheet No", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("loadingsheetId").getValue() != null) {
		          Label l = new Label();
		          l.setValue(getItem(itemId).getItemProperty("loadingsheetId").getValue());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Route", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("route").getValue() != null) {
		          Label l = new Label();
		          Route r =(Route) getItem(itemId).getItemProperty("route").getValue();
		          l.setValue(r.toString());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Origin", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("origin").getValue() != null) {
		          Label l = new Label();
		          Substation s =(Substation) getItem(itemId).getItemProperty("origin").getValue();
		          l.setValue(s.getName());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Destination", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("destination").getValue() != null) {
		          Label l = new Label();
		          Substation s =(Substation) getItem(itemId).getItemProperty("destination").getValue();
		          l.setValue(s.getName());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Date", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("loadDate").getValue() != null) {
		          Label l = new Label();
		          Date date = (Date) getItem(itemId).getItemProperty("loadDate").getValue();
		          l.setValue(app.formatDateWithoutTime(date));
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Reg No", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("bus").getValue() != null) {
		          Label l = new Label();
		          Bus b =(Bus) getItem(itemId).getItemProperty("bus").getValue();
		          l.setValue(b.getRegistrationNumber());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Driver", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("driver").getValue() != null) {
		          Label l = new Label();
		          Employee b =(Employee) getItem(itemId).getItemProperty("driver").getValue();
		          l.setValue(b.getLastname());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Conductor", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("conductor").getValue() != null) {
		          Label l = new Label();
		          Employee b =(Employee) getItem(itemId).getItemProperty("conductor").getValue();
		          l.setValue(b.getLastname());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addGeneratedColumn("Status", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("loadingsheetStatus").getValue() != null) {
		          Label l = new Label();
		          LoadingsheetStatus ls =(LoadingsheetStatus) getItem(itemId).getItemProperty("loadingsheetStatus").getValue();
		          l.setValue(ls.getDescription());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		addStyleName(Runo.TABLE_SMALL+" lst");
		setVisibleColumns(columns);
		setSizeUndefined();
		setSelectable(true);
		setImmediate(true);
		setPageLength(Constants.LOADINGSHEET_TABLE_PAGE_LENGTH);
	}
}